package android.pot.com.smartcar.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.util.DensityUtils;
import android.pot.com.smartcar.util.ImageLoadUtils;
import android.pot.com.smartcar.util.TextCheckUtil;
import android.pot.com.smartcar.widget.RatingBar;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by zhaoshilu on 2017/11/22.
 */
public class ViewHolder
{
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    private Context context;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position)
    {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        this.context = context;
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        // setTag
        mConvertView.setTag(this);
    }


    public int getMPostion(){
        return mPosition;
    }
    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position)
    {
        if (convertView == null)
        {
            return new ViewHolder(context, parent, layoutId, position);
        }
        return (ViewHolder) convertView.getTag();
    }

    public View getConvertView()
    {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId)
    {
        View view = mViews.get(viewId);
        if (view == null)
        {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public ViewHolder setOnClickListener(int viewId,
                                                View.OnClickListener listener)
    {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    public ViewHolder setOnCheckedChangeListener(int viewId,
                                         CompoundButton.OnCheckedChangeListener listener)
    {
        CheckBox view = getView(viewId);
        view.setOnCheckedChangeListener(listener);
        return this;
    }


    public ViewHolder addTextChangedListener(int viewId,
                                                 TextWatcher listener)
    {
        EditText view = getView(viewId);
        view.addTextChangedListener(listener);
        return this;
    }



    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @return
     */
    public ViewHolder setVisibility(int viewId,int visibility)
    {
        View view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    public ViewHolder setHeight(int viewId, int height)
    {
        View view =  getView(viewId);
        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) view.getLayoutParams();
        params.height=height;
        view.setLayoutParams(params);
        return this;
    }
    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @return
     */
    public ViewHolder setsVisibility(int viewId,int visibility)
    {
        View view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    public ViewHolder addTextView(int viewId,List<String> ruleList)
    {
        LinearLayout ll_layout = getView(viewId);
        ll_layout.removeAllViews();
        for (String rule:ruleList){
            TextView textView = new TextView(context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, 15, 0, 0);
            textView.setLayoutParams(lp);
            textView.setText(rule);
            textView.setTextSize(14);
            textView.setTextColor(Color.BLACK);
            textView.setLineSpacing(10,1);
            //3.把textView设置为线性布局的子节点
            ll_layout.addView(textView);
        }
        return this;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text)
    {
        TextView view = getView(viewId);
        view.setText(TextCheckUtil.CheckString(text));
        return this;
    }

    public ViewHolder setXieText(int viewId, String text)
    {
        TextView view = getView(viewId);
        view.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
        view.setText(text);
        return this;
    }


    public ViewHolder setDrawableLeft(int viewId, int resId,int size)
    {
        TextView textDrawable = getView(viewId);

        Drawable drawableLeft = context.getResources().getDrawable(resId);

        textDrawable.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
                null, null, null);
        textDrawable.setCompoundDrawablePadding(DensityUtils.dp2px(size,context));
        return this;
    }
    public ViewHolder setSpannableText(int viewId, SpannableString text)
    {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }
    public ViewHolder setRating(int viewId,int step){
        if(step>5){
            step = 5;
        }
        RatingBar ratingBar =  getView(viewId);
        ratingBar.setClickable(false);
        ratingBar.setStar(step);
        return this;
    }

    /**
     * 为TextView设置颜色
     *
     * @param viewId
     * @param resId
     * @return
     */
    public ViewHolder setTextColor(int viewId, int resId)
    {
        TextView view = getView(viewId);
        view.setTextColor(ContextCompat.getColor(context,resId));
        return this;
    }

    /**
     * 为View设置背景颜色
     *
     * @param viewId
     * @param resId
     * @return
     */
    public ViewHolder setBackGroudColor(int viewId, int resId)
    {
        View view = getView(viewId);
        view.setBackgroundColor(ContextCompat.getColor(context,resId));
        return this;
    }
    /**
     *
     *
     * @param viewId
     * @param resId
     * @return
     */
    public ViewHolder setBackgroundResource(int viewId, int resId)
    {
        View view = getView(viewId);
        view.setBackgroundResource(resId);
        return this;
    }
    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId)
    {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);
        return this;
    }

    public ViewHolder setSelect(int viewId, boolean isSelect)
    {
        CheckBox view = getView(viewId);
        view.setChecked(isSelect);
        return this;
    }
    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param bm
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm)
    {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public ViewHolder setImageByUrl(int viewId, String url)
    {

        ImageLoadUtils.INSTANCE.loadImageView((ImageView) getView(viewId),url);
        return this;
    }
    public ViewHolder setImageByUrlAndDefault(int viewId, String url,int resId)
    {
        ImageLoadUtils.INSTANCE.loadOptionImageView((ImageView) getView(viewId),url,resId);
        return this;
    }
    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @return
     */
    public ViewHolder setImageAutoFit(int viewId, int imgWidth,int imgHeight)
    {
        int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        float ratio=(float) imgWidth/(float) imgHeight;
        int height=(int)(screenWidth/ratio);
        ImageView imageView = (ImageView) getView(viewId);
        ViewGroup.LayoutParams params=imageView.getLayoutParams();
        params.height=height;
        params.width=screenWidth;
        imageView.setLayoutParams(params);
        return this;
    }


    public ViewHolder setMerChatListImageByUrl(int viewId, String url)
    {
        ImageLoadUtils.INSTANCE.loadOptionImageView((ImageView) getView(viewId),url, R.drawable.shape_f5_bg);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public ViewHolder setImageByUrl(int viewId, String url,int resId)
    {

        ImageLoadUtils.INSTANCE.loadOptionImageView((ImageView) getView(viewId),url,resId);
        return this;
    }

    /**
     * 为ImageView设置圆形图片
     *
     * @param viewId
     * @param url
     * @return
     */
    public ViewHolder setCricleImageByUrl(int viewId, String url,int resId)
    {
        ImageLoadUtils.INSTANCE.loadCircleImageView((ImageView) getView(viewId),url,resId);
        return this;
    }

    public int getPosition()
    {
        return mPosition;
    }

}